
import time

from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from tools.base_page import BasePage


class KaoYan(BasePage):

    def kaoyan_find_school(self):
        # 查院校
        find_school_locator = ('id', 'moduleSearchSchoolTitle')
        brochures = ("id", "item_zsjz_tv")  # 招生简章
        # 招生简章内容第一条
        brochures_title = ('id', 'comm_item_news_title')
        self.find(find_school_locator).click()
        self.find(('xpath', '//*[@text="武汉大学"]')).click()
        self.find(brochures).click()
        WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(brochures_title))
        self.elements(brochures_title)[0].click()
        self.back_activity(2)
        return True

    def kaoyan_baolubi(self):
        baolubi_locator = ('id', 'com.znb.zxx:id/item_blb_tv')
        baolubi_title = (
        'xpath', '//*[@resource-id="com.znb.zxx:id/news_recycle"]/android.view.ViewGroup[1]/android.widget.TextView[1]')
        self.find(baolubi_locator).click()
        try:
            self.find(baolubi_title).click()
            self.back_activity(2)
        except:
            self.back_activity(1)
        return True

    def kaoyan_fenshuxian(self):
        fenshuxian = ('id', 'score_line_tv')
        fenshuxian_title = (
        'xpath', '//*[@resource-id="com.znb.zxx:id/news_recycle"]/android.view.ViewGroup[1]/android.widget.TextView[1]')
        self.find(fenshuxian).click()
        self.find(fenshuxian_title).click()
        self.back_activity(2)
        return True

    def kaoyan_major_mulu(self):
        school_major_more = ('id', 'school_major_more')
        first_major = ('id', 'com.znb.zxx:id/right_title')
        too_major = ('xpath',
                     '//*[@resource-id="com.znb.zxx:id/recycler"]/android.view.ViewGroup[1]/android.widget.TextView[1]')  # 一级学科
        self.find(school_major_more).click()
        self.find(first_major).click()
        self.find(too_major).click()
        self.back_activity(3)
        return True

    def school_news(self):
        news_title = ('xpath',
                      '//*[@resource-id="com.znb.zxx:id/school_news_recycle"]/android.view.ViewGroup[1]/android.widget.TextView[1]')
        cat_more = ('id', 'school_news_more')
        news_list_first_title = ('id', 'comm_item_news_title')
        time.sleep(2)
        self.go_swipe(0.5, 0.5, 0.9, 0.1)
        time.sleep(1)
        self.find(news_title).click()
        self.back_activity(1)
        self.find(cat_more).click()
        WebDriverWait(self.driver, 20).until(expected_conditions.visibility_of_element_located(news_list_first_title))
        self.elements(news_list_first_title)[0].click()
        self.page(self.index)  # 回到app首页

    def find_major(self):
        find_major_locator = ('xpath', '//*[@text="查专业"]')
        major_locator = ('id', 'com.znb.zxx:id/right_title')
        too_major = ('xpath', '//*[@resource-id="com.znb.zxx:id/recycler"]/android.view.ViewGroup[1]')
        school_ku = ('id', 'com.znb.zxx:id/get_tv')  # 院校库

        self.find(find_major_locator).click()
        self.find(major_locator).click()
        self.find(too_major).click()
        try:
            self.find(school_ku).click()
        except:
            print("没有弹窗!")
        A_class = ('id', 'type_A')
        B_class = ('id', 'type_B')
        C_class = ('id', 'type_C')
        type_Other = ('id', 'type_Other')

        self.find(B_class).click()
        self.find(C_class).click()
        self.find(type_Other).click()
        self.find(A_class).click()

    def scool_region(self):
        school_title = ('xpath', '//*[@resource-id="com.znb.zxx:id/school_recycle"]/android.view.ViewGroup[1]')
        school_name = ('id', 'school_name_tv')
        self.find(school_title).click()
        time.sleep(3)
        # print(self.driver.page_source)
        self.find(school_name).click()
        self.driver.back()
        time.sleep(1)
        self.go_swipe(0.5, 0.5, 0.9, 0.1)
        time.sleep(1)
        self.page(("id", "com.znb.zxx:id/search_img"))
        return True

    def major_seach(self):  # 查专业搜索
        seacher_locator = ('id', 'search_img')
        input_seacher = ('id', 'search_edit_text')
        seach_major = ('id', 'item_title')

        self.find(seacher_locator).click()
        self.find(input_seacher).send_keys('哲学')
        time.sleep(1)
        self.elements(seach_major)[0].click()
        self.page(("xpath", "//*[@text='首页']"))
        return True

    def find_paper(self):
        find_paper_locator = ('xpath', '//*[@text="查真题"]')
        class_paper = ('xpath', '//*[@resource-id="com.znb.zxx:id/title_container"]/android.widget.FrameLayout[2]')
        start_do_paper = ('id', 'examStartBtn')  # 开始做题
        next_paper = ('id', 'examNextQuestionViewTv')
        submit_exam = ('id', 'com.znb.zxx:id/submit_exam_btn')  # 交卷
        jiaoquan_cancel = ('id', 'com.znb.zxx:id/dialog_undone_warn_cancel')  # 交卷取消
        dialog_done_warn_confirm = ('id', 'dialog_done_warn_confirm')  # 交卷取消

        # 选项1、2、3
        option = ('id', 'item_option_content')
        questionCard = ('id', 'questionCardView')

        self.find(find_paper_locator).click()
        self.find(class_paper).click()
        for i in range(1, 10):
            locator = ('xpath', '//*[@resource-id="com.znb.zxx:id/paper_recycle"]/android.view.ViewGroup{}'.format([i]))
            location = ("id", "examStartBtn")
            text_name = '线上答题'
            text_value = '重新做题'
            if self.find_locator_and_back(locator, location, text_name, location, text_value) == True:
                break
        for num in range(0, 3):
            self.elements(option)[num].click()
            time.sleep(1)
        locator_list = [next_paper, questionCard, submit_exam, jiaoquan_cancel, questionCard, submit_exam,
                        dialog_done_warn_confirm]
        for level in locator_list:
            self.find(level).click()
            time.sleep(1)
        # self.find(next_paper).click()
        # self.find(questionCard).click()
        # self.find(submit_exam).click()
        # self.find(jiaoquan_cancel).click()
        # self.find(questionCard).click()
        # self.find(submit_exam).click()
        # self.find(dialog_done_warn_confirm).click()

    def is_exist_report_card(self):
        '''
        判断是否交卷成功
        :return:
        '''
        report_card = ('id', 'pager_title')
        if self.is_element(report_card):
            self.page(self.index)
            return True
        else:
            self.page(self.index)
            return False

    def find_course(self):
        find_course_locator = ('xpath', '//*[@text="查课程"]')
        major_name = ("id", "com.znb.zxx:id/right_title")
        major_option = ('id', 'indicatorTitleTv')

        self.find(find_course_locator).click()
        for num in range(1, 5):
            self.elements(major_option)[num].click()
            time.sleep(1)
        self.find(major_name).click()
        self.page(self.index)

    def get_coupon(self):

        # find_course_locator = ('xpath','//*[@resource-id="com.znb.zxx:id/paper_recycle"]/android.view.ViewGroup[1]/android.widget.TextView[1]')
        # self.find(find_course_locator).click()
        close = ('id', 'com.znb.zxx:id/cancelImg')
        get_conpon = ("id", 'courseCouponTitle')
        for i in range(1, 11):
            locator = ("xpath",
                       '//*[@resource-id="com.znb.zxx:id/paper_recycle"]/android.view.ViewGroup{}/android.widget.TextView[1]'.format(
                           [i]))
            location = ('id', 'com.znb.zxx:id/courseCouponTitle')
            text_name = ('优惠')
            if self.find_locator_and_back(locator, location, text_name, location) == True:
                break
        try:
            self.find(get_conpon).click()
            self.find(close).click()
        except:
            self.find(close).click()
        self.page(("xpath", "//*[@text='首页']"))
        return True

    def time_string(self):
        cat_more = ('id', 'timeLineMoreTv')
        self.find(cat_more).click()
        time.sleep(3)
        self.driver.back()
        time.sleep(1)
        self.swipLeft()

    def goto_kaoyan_open_course(self):
        cat_more_open_course = (
            'xpath', '//*[contains(@resource-id,"com.znb.zxx:id/contentTv") and contains(@text,"查看更多公开课")]')
        study_locator = ('id', 'com.znb.zxx:id/dialog_receive_btn')  # 弹窗立即学习
        course_title = ('id', 'com.znb.zxx:id/courseNameTv')  # 课程详情的课程标题
        courese_list_title = ('xpath',
                              '//*[@class="androidx.recyclerview.widget.RecyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')  # 课程列表中的课程标题

        time.sleep(2)
        self.swipeUp()
        # self.go_swipe(0.5, 0.8, 0.5, 0.5)
        self.find(cat_more_open_course).click()
        self.find(courese_list_title).click()
        self.back_activity(2)

    def customization_target(self):
        myTargetEnterIv = ('id', 'myTargetEnterIv')  # 考编目标入口
        year_tv = ('id', 'year_tv')
        ky_type_tv = ('id', 'ky_type_tv')
        ky_xk_type_tv = ('id', 'ky_xk_type_tv')
        ky_first_xk_tv = ('id', 'ky_first_xk_tv')
        ky_major_tv = ('id', 'ky_major_tv')
        ky_school_tv = ('id', 'ky_school_tv')
        finish = ('text', '完成')
        confirm = ('xpath', '//*[@text="确定"]')
        find_school = ('id', 'com.znb.zxx:id/moduleSearchSchoolTitle')

        self.find(find_school)
        self.go_swipe(0.5, 0.8, 0.5, 0.2)
        self.find(myTargetEnterIv).click()
        ky_list = [year_tv, ky_type_tv, ky_xk_type_tv, ky_first_xk_tv, ky_major_tv, ky_school_tv]
        time.sleep(5)
        for ky in ky_list:
            self.click_customization_target(ky, finish)
        self.find(confirm).click()

    def click_customization_target(self, location, finish):
        self.find(location).click()
        time.sleep(2)
        self.find_element(finish).click()
        time.sleep(2)

    def exist_customization_target(self):
        myTargetTitle = ('id', 'com.znb.zxx:id/myTargetTitle')
        if self.find(myTargetTitle).text != None:
            self.page(self.index)
            return True
